package com.zhenwei.zscm.repo;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.base.MybatisWrapper;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.ProductPartEntity;
import com.zhenwei.zscm.mapper.IProductPartMapper;
import com.zhenwei.zscm.mappings.ProductPartMapping;
import com.zhenwei.zscm.model.dto.productPart.ProductPartDTO;
import com.zhenwei.zscm.model.vo.productPart.ProductPartVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author DuckyYang
* @description 针对表【product_part】的数据库操作Service实现
* @createDate 2025-05-22 09:41:03
*/
@Service
public class ProductPartRepo extends ServiceImpl<IProductPartMapper, ProductPartEntity> {

    /**
     * 保存产品的备件配置
     * @param list list
     * @return bool
     */
    @Transactional
    public boolean createBatch(List<ProductPartDTO> list) {
        List<ProductPartEntity> entityList = Linq.mapList(list, ProductPartMapping.INSTANCE::dto2Entity);
        entityList.forEach(x-> x.setGuid(IdUtil.simpleUUID()));
        return this.saveBatch(entityList);
    }

    public boolean create(ProductPartVO vo) {
        ProductPartEntity e = ProductPartMapping.INSTANCE.vo2Entity(vo);
        if("".equals(e.getGuid())){
            e.setGuid(IdUtil.simpleUUID());
        }
        return this.save(e);
    }

    public boolean update(ProductPartVO vo) {
        var u = new LambdaUpdateWrapper<ProductPartEntity>()
                .eq(ProductPartEntity::getGuid, vo.getGuid())
                .eq(ProductPartEntity::getDeleted, false)
                .set(ProductPartEntity::getPartName, vo.getPartName())
                .set(ProductPartEntity::getPrice, vo.getPrice());
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }

    /**
     * 获取产品的备件列表
     * @param productCode 产品id
     * @return List<ProductPartDTO>
     */
    public List<ProductPartDTO> getProductPartByProductCode(String productCode) {
        var q = new LambdaQueryWrapper<ProductPartEntity>()
                .eq(ProductPartEntity::getProductCode, productCode)
                .eq(ProductPartEntity::getDeleted, false)
                .orderByAsc(ProductPartEntity::getId);
        List<ProductPartEntity> list = this.list(q);
        return Linq.mapList(list, ProductPartMapping.INSTANCE::entity2Dto);
    }
    public List<ProductPartDTO> getAll(){
        var q = new LambdaQueryWrapper<ProductPartEntity>()
                .eq(ProductPartEntity::getDeleted, false);
        List<ProductPartEntity> list = this.list(q);
        return Linq.mapList(list, ProductPartMapping.INSTANCE::entity2Dto);
    }
    /**
     * 删除产品的备件列表
     * @param guid 配件信息的主键ID
     * @return bool
     */
    public boolean delete(String guid) {
        var u = new LambdaUpdateWrapper<ProductPartEntity>()
                .eq(ProductPartEntity::getGuid, guid)
                .eq(ProductPartEntity::getDeleted, false)
                .set(ProductPartEntity::getDeleted, true);
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }

    /**
     * 检查产品是否已经添加过相同名称的配件
     * @param productCode 产品code
     * @return bool
     */
    public boolean checkProductPartExists(String productCode, String partName) {
        var q = new LambdaQueryWrapper<ProductPartEntity>()
                .eq(ProductPartEntity::getProductCode, productCode)
                .eq(ProductPartEntity::getPartName, partName)
                .eq(ProductPartEntity::getDeleted, false);
        return this.exists(q);
    }

    /**
     * 检查产品是否已经添加了除了当前GUID配件外重名的配件
     * @param vo vo
     * @return bool
     */
    public boolean checkProductPartExists(ProductPartVO vo) {
        var q = new LambdaQueryWrapper<ProductPartEntity>()
                .eq(ProductPartEntity::getProductCode, vo.getProductCode())
                .eq(ProductPartEntity::getPartName, vo.getPartName())
                .ne(ProductPartEntity::getGuid, vo.getGuid())
                .eq(ProductPartEntity::getDeleted, false);
        return this.exists(q);
    }
}




